const express = require('express');
const router = require('./routes');

// 全局异常处理
process.on('unhandledRejection', (reason, p) => {
  console.log("Unhandled Rejection at: Promise ", p, " reason: ", reason);
  // application specific logging, throwing an error, or other logic here
});

process.on('uncaughtException', function (err) {
  console.log('Caught exception: ', err);
});

const app = express();
app.use(express.urlencoded({ extended: false }));
app.use(express.json());

app.use(router);

app.use((err, req, res, next) => {
  console.log(err);
  if (res.headersSent) {
    return next(err);
  }

  res.send({
    errCode: 1,
    errMsg: err.message
  });
});

const EXPRESS_PORT = process.env.EXPRESS_PORT;
const SOCKET_PORT = process.env.SOCKET_PORT;

app.listen(EXPRESS_PORT, () => {
  console.log(`Http Server is running at http://localhost:${EXPRESS_PORT}`);
  console.log(`Socket.IO Server is running at http://localhost:${SOCKET_PORT}`);
});